package com.aidex.biz.service.impl;

import com.aidex.biz.domain.BillInfo;
import com.aidex.biz.domain.BillInfoDetails;
import com.aidex.biz.mapper.BillInfoMapper;
import com.aidex.biz.service.BillInfoDetailsService;
import com.aidex.biz.service.BillInfoService;
import com.aidex.biz.service.OrderService;
import com.aidex.common.annotation.DataScope;
import com.aidex.common.core.domain.entity.Company;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.common.utils.NumberUtils;
import com.aidex.common.utils.StringUtils;
import com.aidex.system.service.CompanyService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 对账信息Service业务层处理
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-09-27
 */
@Service
@Transactional(readOnly = true)
public class BillInfoServiceImpl extends BaseServiceImpl<BillInfoMapper, BillInfo> implements BillInfoService
{
    private static final Logger log = LoggerFactory.getLogger(BillInfoServiceImpl.class);
    @Autowired
    private CompanyService companyService;
    @Autowired
    private OrderService orderService;

    @Autowired
    private BillInfoService billInfoService;
    @Autowired
    private BillInfoDetailsService billInfoDetailsService;
    /**
     * 获取单条数据
     * @param billInfo 对账信息
     * @return 对账信息
     */
    @Override
    public BillInfo get(BillInfo billInfo)
    {
        BillInfo dto = super.get(billInfo);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 对账信息id
     * @return 对账信息
     */
    @Override
    public BillInfo get(String id)
    {
        BillInfo dto = super.get(id);
        return dto;
    }


    /**
     * 查询对账信息列表
     * @param billInfo 对账信息
     * @return 对账信息
     */
    @Override
    @DataScope(userAlias = "a",deptAlias = "a")
    public List<BillInfo> findList(BillInfo billInfo)
    {
        return super.findList(billInfo);
    }

    /**
     * 分页查询对账信息列表
     * @param billInfo 对账信息
     * @return 对账信息
     */
    @Override
    @DataScope(userAlias = "a",deptAlias = "a")
    public PageInfo<BillInfo> findPage(BillInfo billInfo)
    {
        return super.findPage(billInfo);
    }

    /**
     * 保存对账信息
     * @param billInfo
     * @return 结果
     */
    @Override
    public boolean save(BillInfo billInfo)
    {
        return super.save(billInfo);
    }


    /**
     * 删除对账信息信息
     * @param billInfo
     * @return 结果
     */
    @Override
    public boolean remove(BillInfo billInfo)
    {
        return super.remove(billInfo);
    }

    /**
     * 批量删除对账信息
     * @param ids 需要删除的对账信息ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteBillInfoByIds(String[] ids)
    {
        return mapper.deleteBillInfoByIds(ids);
    }

    /**
     * 获取最大编号
     * @param billInfo 对账信息
     * @return 结果
     */
    @Override
    public int findMaxSort(BillInfo billInfo)
    {
        return NumberUtils.nextOrder(mapper.findMaxSort(billInfo));
    }

    /**
     * 每日23点40 统计各个商家账单信息
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public Integer getDayBillDetails() {
        List<Company> companies=companyService.findList(new Company());
        try {
            for (Company company:companies){
                BillInfo billInfo=new BillInfo();
                billInfo.setId(StringUtils.Getnum());
                billInfo.setIsNewRecord();
                billInfo.setCompanyId(company.getId());
                billInfo.setStatus("0"); //
                // billInfo.setType("0"); // 账单类型 0 洗护 1 商城
                BillInfoDetails billInfoDetailsParams=new BillInfoDetails();
                billInfoDetailsParams.setBillId(billInfo.getId());
                billInfoDetailsParams.setCompanyId(company.getId());
                //统计当天、某个店铺 待对账信息
                BillInfoDetails sumBillInfoDetails= billInfoDetailsService.sumToDayBillDetails(billInfoDetailsParams);
                billInfo.setMoney(sumBillInfoDetails.getSumMoney());
                billInfoService.save(billInfo);
                //对账完成后修改待对账
                billInfoDetailsService.updateSumToDayBillDetails(billInfoDetailsParams);
                Thread.sleep(2000);
            }
        }catch (Exception exception){
            log.error("对账单生成出错", exception.getMessage());
            return -1;
        }
        return 1;
    }
}
